package com.acceptable.qucun.generic.config;

import com.acceptable.qucun.generic.entity.UrlUserItem;
import com.acceptable.qucun.generic.interceptor.RedisInterceptor;
import com.acceptable.qucun.user.interceptor.AccountBoundInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfig implements WebMvcConfigurer{
    @Autowired private RedisInterceptor redisInterceptor;
    @Autowired private AccountBoundInterceptor emailInterceptor;
    @Autowired private UrlUserItem urlUserItem;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 查找用户是否绑定
        registry.addInterceptor(emailInterceptor)
                .addPathPatterns(
                    "/user/reset_pwd", "/user/register",
                    "/user/login", "/user/logout",
                    "/user/head_img_show", "/user/head_img_upload", "/user/head_img_download/**",
                    "/user/update_info", "/user/init_info", "/user/init_user_info",
                    "/user/change_email_verify",
                    "/user/drop_account");

        // 进行redis存入
        registry.addInterceptor(redisInterceptor)
                .addPathPatterns(
                    "/user/verify_code",
                    "/file/merge_file",  "/file/file_exist");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
//        System.out.println("后端allowHost: " + urlUserItem.getAllowHost());
        registry.addMapping("/**")
                .allowCredentials(true) // 携带Cookie
                .allowedOrigins(urlUserItem.getAllowHost())
                .allowedHeaders("*")
                .allowedMethods("*");
    }

//    @Override
//    public void configurePathMatch(PathMatchConfigurer configurer) {
//        configurer.getUrlPathHelper().setRemoveSemicolonContent(false);
//    }
}
